package com.example.nacos.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.example.nacos.cache.CacheData;
import com.example.nacos.entity.Goods;
import com.example.nacos.excelvo.HcExcel;
import com.example.nacos.excelvo.RetExcel3;
import com.example.nacos.listener.ReadExcelListener;
import com.example.nacos.listener.ReadHcExcelListener;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@RestController
@RequestMapping("excel")
public class HandleExcelController {


    /**
     * 核对结果
     */
    List<RetExcel3> retExcelList ;
    /**
     * 病人耗材记录
     */
    Map<String,HcExcel> hcExcelMap ;


    @PostMapping("/upload")
    public String handleFileUpload(@RequestPart("file") MultipartFile file) {
        try {

            Set<String> needFillGoodsData = new HashSet<>();
            List<String> noData = new ArrayList<>();
            List<String> finishData = new ArrayList<>();
            ReadExcelListener listener = new ReadExcelListener();
            EasyExcel.read(file.getInputStream(), RetExcel3.class, listener)
                    .sheet()
                    .doRead();
            retExcelList =  listener.allData();

            if (hcExcelMap != null){
                for (int i = 0; i < retExcelList.size(); i++) {
                    RetExcel3 ite = retExcelList.get(i);
                      String je = Objects.nonNull( ite.getHjjey()) ? ite.getHjjey().split("\\.")[0] : "0";
                    String key = ite.getName() +"-"+je;
                    HcExcel hcExcel =  hcExcelMap.get(key);
                    Goods goods = CacheData.goodsMapKeyYbm.get(ite.getYbgbm());
                    if(Objects.isNull(hcExcel) && Objects.isNull(goods)){
                        noData.add(key);
                    }else {
                        goods = Objects.isNull(goods)&&Objects.nonNull(hcExcel) ? CacheData.goodsMap.get(hcExcel.getGwls()) : goods;
                        if(Objects.nonNull(goods)){
                            ite.setGgTwo(goods.getOutLookc());
                            ite.setXh(goods.getModel());
                            ite.setYbm(goods.getDrugCode());
                            finishData.add(ite.getName());
                        }else {
                            needFillGoodsData.add(hcExcel.getGwls());
                        }
                    }
                }
            }


        System.out.println("数据大小："+retExcelList.size());
            System.out.println(JSON.toJSONString(retExcelList));
            System.out.println("-----姓名和金额没有匹配上的数据----------:"+noData.size());
            System.out.println(JSON.toJSON(noData));
            System.out.println("---------------需要补充挂网流水号的产品---------------------");
            System.out.println(JSON.toJSON(needFillGoodsData));
            System.out.println("-----------匹配成功数据---------："+finishData.size());
            System.out.println(finishData);
            //写数据
            String fileName = "D:\\核对结果-" + System.currentTimeMillis()/1000 +".xlsx";
            File exportFile = new File(fileName);
            // 这里需要指定写用哪个class去写，然后写到第一个sheet，名字为模板，然后文件流会自动关闭
            EasyExcel.write(exportFile, RetExcel3.class).sheet("sheet").doWrite(retExcelList);

        } catch (IOException e) {
            e.printStackTrace();
            return "Error occurred while processing the file";
        }
        return "D:\\校对结果表.xlsx";
    }


    /**
     * 第一步  上传耗材
     * @param file
     * @return
     */
    @PostMapping("/uploadXh")
    public String handleXH(@RequestPart("file") MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            ReadHcExcelListener listener = new ReadHcExcelListener();
            EasyExcel.read(file.getInputStream(), HcExcel.class, listener)
                    .sheet()
                    .doRead();
            hcExcelMap =  listener.allData();
            System.out.println("数据大小："+hcExcelMap.size());
            System.out.println(JSON.toJSONString(hcExcelMap.keySet()));
        } catch (IOException e) {
            e.printStackTrace();
            return "Error occurred while processing the file";
        }
        return "";
    }




}
